/**
  ******************************************************************************
  * @file    func.c
  * @author  测试1组（houtiezhu）
  * @version v0
  * @date    09-07-2019
  *
  * @verbatim
  **/
/* Includes ------------------------------------------------------------------*/

#include "define.h"
#include "disp.h"
#include "power.h"
#include "clk.h"
#include "gpio.h"
#include "time.h"
#include "uart.h"
#include "spk.h"
#include "flash.h"
#include "adc.h"
#include "algo.h"
#include "bp.h"
#include "func.h"
#include "op.h"
#include "pwm.h"
#include "string.h"

uint8 InfDefFlag;
uint8   OneSecDeflated;
uint16  DiaMeanEnd;
uint8 UserBak;
uint16 valveoffset;
uint8 VavleTestFlag;
uint8 limit;
uint8 TimeBak[5];
uint16  DiaMeanEnd;
//uint8   MemoryEndIndex;
uint8   DeflateMemory;
uint16 SetKeyTimew;

volatile union byte_bit		initstates;
volatile union byte_bit		ibuffer;
volatile union	word_data	AA1w;

e_voice_c_e_ma_switch_t e_voice_c_e_ma_switch = E_ENG;

const uint8  MonthDay[]	=	{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
								//1	 2	 3 	 4 	 5 	 6 	 7 	 8 	 9	 10	 11	 12

const uint8  ParameterArrayLow[]	=	{0,	0,	1,	0,	0,	0,	0,		0,	0,		0	};
const uint8  ParameterArrayHigh[]	=	{8,	6,	12,	14,	10,	10,255,	255,	255,	255};



void initialHW(void)
{
	//Osc_Setup_CR();
	//RCC_ClockOut(RCC_CLKOUT_SYSCLK,ENABLE);//RCC_CLKOUT_ILRC
	UserPower_Init();
	UserVIC_Init();
	UserLCD_Init();
	UserGPIO_Init();
	UserLCD_Init();
	UserSARADC_LBT_Init();
	UserSDADCInit();
    UserEXINT_Init();
	UserTimer0_Init();
	//UserRtc_Init();
	User_Uart0_Init();
	BZ_Init();
	UserPwm1_Init();
	if(LinearValveFlag)	UserPwm0_Init();

/*	if(RCSensorType&&LinearValveFlag)
	{	PWM0_Init();	}								//R type sensor P0.6 is linear valve
	else
	{	TC0_Init();		}								//C type senosr P0.6 is valve;   TC0 is sensor counter

	*(uint32_t*)0xE000E100 |=0xffffffff;
	*(uint32_t*)0xE000E140 |=0xffffffff;
*/
}




void initial(void)					//
{
#if 1
	initialHW();
	readfunction();
	if(KeyNumber==12) YEARINIT=20;
	else YEARINIT=VerYear;//临时
	if(KeyNumber==12) MaxYear=40;
	else if(KeyNumber==11) MaxYear=39;
	else MaxYear=99;

	if(KeyNumber==11||KeyNumber==12) {ModifyFlag=1;AUTOOFFTIME=0xBB8;}
	else AUTOOFFTIME=0x2328;

	if(UartType==6) AUTOOFFTIME=0XBB8;

	if(StartBZ) BUZZER_EN;
	SysStateFlag.b = 0x00;		//initial System State Flag
	OneShotFlag.b = 0x00;			//initial One Shot Flag
	MeasureStateFlag.b = 0x00;//initial Measure state Flag
	ResultCode = 0x00;				//initial Result Code
	STATEFlag1.b=0;
	TimeBase.b=0;
	OneShotFlag.b=0;
	SysStateFlag.b=0;
	SysStateFlag2.b=0;
	SystemSetFlag6.b=0;
	SystemSetFlag5.b=0;
	Year.b=YEARINIT;					//initial date and timer data
	Month=MONTHINIT;
	Day=DAYINIT;
	Hour=HOURINIT;
	Minute=MINUTEINIT;

	TimeBak[0]=YEARINIT;
	TimeBak[1]=MONTHINIT;
	TimeBak[2]=DAYINIT;
	TimeBak[3]=HOURINIT;
	TimeBak[4]=MINUTEINIT;
	UserBak=UserA;

	MemoryEndIndex=1;
	MemoryFull=0;
	ResultDataS = 0;
	ResultDataD = 0;
	ResultDataH = 0;
	TimeInterval=0;
	LastDIA=0;
	LastSYS=0;
	IsCalibrate=0;
	IsCALMode=0;
	DeflateMemory=250;
	ConnectFlag=0;
//	CheckEepromIsOk();
	AlarmN=0;
	AlarmHour[0]=8;
	AlarmHour[1]=18;
	AlarmMinute[0]=0;
	AlarmMinute[1]=0;
	AlarmOnOff[0]=0;
	AlarmOnOff[1]=0;

	LongKeyCount[0]=0;
	LongKeyCount[0]=0;
	LongKeyCount[0]=0;
	LongKeyCount[0]=0;
	LongKeyCount[0]=0;
	LongKeyFlag.b=0;
	KeyModeFlag.b=0;
	SetKeyTimew=0;

#endif
}
//*************************************
//*************************************
void InitialLoop(void)
{
#if 1
	unsigned char  i;
	unsigned char  readeepromdata=0;
	VavleTestFlag=0;
	initialHW();
	KeyModeFlag.b=0;
	SystemSetFlag.b=0;
	SystemSetFlag2.b=0;
	SystemSetFlag3.b=0;
	initstates.b=0;
	ShowPressCount=0;
	MemoryMode=0;
	LBTCount=0;
	IsPump=0;
	BPTestFlag=0;
	FreWho130Flag = 0;
	UparmScaleValue = 1;
	FuzzyMaxBackup = 1;
	TestValue = 7;

//	CheckMemDataIsOK(); //check  user mem is ok?
	i=0;
	readeepromdata=0;
	do
	{
		EEPROMin=UserSetAdd;					//UserSetAdd是设定值
		readM24L16();//JACK
		readeepromdata++;
		i++;
	}while((ReadEepromError==1)&&(readeepromdata<3));
	if(ReadEepromError==1){goto EepromError;}  //读三次备份值,如果CS不对,则用默认值.

	UserSetFlag.b=inbyte[0];
	VoiceLevel=inbyte[1]& 0x0f;
    e_voice_c_e_ma_switch = ((inbyte[1] & 0xf0) >> 4);
    if (e_voice_c_e_ma_switch >= E_VOICE_MAX)
    {
        e_voice_c_e_ma_switch = E_ENG;
    }

	if(VoiceLevel>3)
    {
        if (VoiceType==1&&VoiceType2==3)
        {
            VoiceLevel=1;//中英马来西亚语 默认 音量1
        }
        else
        {
            VoiceLevel=3;
        }
    }
	goto initialover;
EepromError:	//默认用户功能
			mmHgkPaFlag=1;
			UserA=1;
			VoiceEnable=1;
            if (VoiceType==1&&VoiceType2==3)
            {
                VoiceLevel=1;//中英马来西亚语 默认 音量1
            }
            else
            {
                VoiceLevel=3;
            }
            e_voice_c_e_ma_switch = E_ENG;
initialover:
			readfunction();//读血压计功能
			//功能限制
			if(BPLevelType>ParameterArrayHigh[0]) BPLevelType=1;
			if(BPLevelType==2) {VoiceEnable=0;VoiceSetEnable=0;}	//五格WHO不报语音
			if(UartType>ParameterArrayHigh[1]) UartType=2;
			if(KeyNumber>ParameterArrayHigh[2]||KeyNumber==0) KeyNumber=3;
			if(KeyNumber==2||KeyNumber==12) ThreeKeyFlag=0;
			else ThreeKeyFlag=1;

			if(VoiceType==2||VoiceType==3||VoiceType==4||VoiceType==5||VoiceType==7||VoiceType==8||VoiceType==9||VoiceType==11) {UnitSetEnable=0;}		//英土双语无单位设定
			if(VoiceType==0) {VoiceEnable=0;VoiceSetEnable=0;}		//0无语音
			else
			{
                if ((VoiceType==1&&VoiceType2 == 3) && VoiceLevel == 0)//中英马三语 如果音量为0则不进行播报
                {
                    VoiceEnable = 0;
                }
                else
                {
                    if(!VoiceSetEnable) VoiceEnable=1;
                }
			}

			if(VoiceType==13) BPLevelType=1;

			if(UnitSetEnable==0) mmHgkPaFlag=1;	//不设定单位则默认mmhg

			if(KeyNumber==1)
			{
				NoTimer=1;
				TwoUser=0;
			}

			switch(MemorySetType)
			{
				case 0:
					MemorySet=30;
					break;
				case 1:
					MemorySet=60;
					break;
				case 2:
					MemorySet=90;
					break;
				case 3:
					MemorySet=99;
					break;
				case 4:
					MemorySet=100;
					break;
				case 5:
					MemorySet=120;
					break;
				case 6:
					MemorySet=150;
					break;
				case 7:
					MemorySet=180;
					break;
				case 8:
					MemorySet=199;
					break;
				case 9:
					MemorySet=200;
					break;
				case 10:
					MemorySet=240;
					break;
				case 11:
					MemorySet=500;
					break;
				default:
					MemorySet=90;
			}
			DeflateSetting=30;
			DeflateSetting+=DeflateValueType*5;

			if(InfDefFlag) InflateRate=50;
			else InflateRate=200;

			if(InfDefFlag) limit=20;
			else limit=30;

			if(RCSensorType) UserSDADCInit();//ADC_Init();

			if(RCSensorType==0) LinearValveFlag=0;	//电容式传感器只能用快速阀
			if(TwoUser==0){UserA=1;}

			if(AverageType)		AverageSet=3;
			else				AverageSet=MemorySet;

			//ES0	=	Disable;	//RXIDisable

			if(RCSensorType&&LinearValveFlag)
				{	/*PWM0_Init();*/	}								//R type sensor P0.6 is linear valve
			else
				{	/*TC0_Init();	*/	}								//C type senosr P0.6 is valve;   TC0 is sensor counter

//**************************************
	UserLCD_Init();//LCD_Init();
	spandivid();
	for(validcount=0;validcount<10;validcount++)  //leakage ram ini
		{LeakageP[validcount]=0;}
	for(validcount=0;validcount<63;validcount++)  //leakage ram ini
		{PulseRam[validcount].b[0]=0;PulseRam[validcount].b[1]=0;}
	AutoKeyCount[0]=0;
	AutoKeyCount[1]=0;

	if(UartType==5)
	{
		//EEPROMin=WorkModeAdd;
		//readM24L16();//JACK
		EEPROMin=WorkModeAdd;
		readM24L16();
		if(inbyte[6]==0xac)
		{
			WorkMode=inbyte[0];
		}
		else WorkMode=0;

		if(WorkMode>1) WorkMode=0;
	}

	if(LinearValveFlag)
	{
		EEPROMin=valveoffsetAdd;
		readM24L16();//JACK
		if(inbyte[6]==0xac)
		{
			valveoffset=inbyte[0];
			valveoffset*=0x100;
			valveoffset+=inbyte[1];
		}
		else valveoffset=400;
	}


#endif
}





void AutoCALMode(void)
{
#if 1
	uint8 i;
	unsigned int  ZeroCountwTemp;
	IsCalibrate=1;
	if(UparmWWFlag)
		{
			if(RCSensorType) 	AA1=A1UPR;
			else				AA1=AA1UP;
			B1=B1UP;
			C1=C1UP;
		}
	else
		{
			if(RCSensorType) 	AA1=A1WWR;
			else				AA1=A1WW;
			B1=B1WW;
			C1=C1WW;
		}
	MemoryMode=0;
	mmHgkPaFlag=1;
	DarkAllIcon();
//	DisplayHalfByte(0xc,SYSHundred);	//2-C
//	DisplayHalfByte(0xa,SYSTen);		//1-A
//	DisplayHalfByte(0x13,SYSSingle);  	//0-L
	DisplayHalfByte(0xc,SYSTen);		//2-C
	DisplayHalfByte(0xa,SYSSingle);		//1-A
	DisplayHalfByte(0x13,MemorySetSingle);  	//0-L

	while(!KEY_POWER);
	delay(1);
	while(!KEY_POWER);
	nopdelay();
	BB=POWER | VALVE;
	iocontrolon();
	delay(10);
	zerotracing();
 	ZeroCountwTemp=ZeroCountw;
	DisplayToPUL(1);
	do{
			calpressure();
			Calshowpres();
			LCD5bitDisplayT(SensorCountw);
			delay(4);
		}while(KEY_POWER&&KEY_TIME);
	if(!KEY_TIME){goto ChangeFunction;}
	while(!KEY_POWER);
	delay(1);
	while(!KEY_POWER);
	if(RCSensorType) goto RsensorCalibrate;
//******************************************************* C type sensor calibrate pressure
//cal00:
		if((ZeroCountwTemp>SensorCountw)&&(SensorCountw>0x100))
			{
				AA1 =(ZeroCountwTemp-SensorCountw)/SpanValve;
				ZeroCountwTemp=SensorCountw;
				if((PressuremmHgw>(1000+CALPressureLimit))||(PressuremmHgw<(1000-CALPressureLimit))) {goto calerror;}//20121002
			}
		else {goto calerror;}
		calpressure();
		Calshowpres();
		LCD5bitDisplayT(AA1);	//显示A1
		BUZZER_EN;
		delay(64);
		BUZZER_DIS;

		DisplayToPUL(2);			//校正200
		do{
			calpressure();
			Calshowpres();
			delay(4);
     	}while(KEY_POWER);
     	delay(8);
     	do{delay(8);}while(!KEY_POWER);
     	delay(8);
     	do{delay(8);}while(!KEY_POWER);
//cal200:
		if((ZeroCountwTemp>SensorCountw)&&(SensorCountw>0x100))
			{
				B1 =(ZeroCountwTemp-SensorCountw)/SpanValve ;
				ZeroCountwTemp=SensorCountw;
				if((PressuremmHgw>(2000+CALPressureLimit))||(PressuremmHgw<(2000-CALPressureLimit))) {goto calerror;}//20121002
			}
		else {goto calerror;}
		calpressure();
		Calshowpres();
		LCD5bitDisplayT(B1);	//显示B1
		BUZZER_EN;
		delay(64);
		BUZZER_DIS;

		DisplayToPUL(3);			//校正300
		do{
     	  calpressure();
     	  Calshowpres();
     	  delay(4);
     	}while(KEY_POWER);
		delay(8);
		do{delay(8);}while(!KEY_POWER);
		delay(8);
		do{delay(8);}while(!KEY_POWER);

//cal300:
		if((ZeroCountwTemp>SensorCountw)&&(SensorCountw>0x100))
			{
				C1 =(ZeroCountwTemp-SensorCountw)/SpanValve;
				ZeroCountwTemp=SensorCountw;
				if((PressuremmHgw>(3000+CALPressureLimit))||(PressuremmHgw<(3000-CALPressureLimit))) {goto calerror;} //20121002
			}
		else {goto calerror;}
		calpressure();
		Calshowpres();
		LCD5bitDisplayT(C1);	//显示C1
		BUZZER_EN;
		delay(64);
		BUZZER_DIS;
		goto calover;
//*********************************************************C type sensor calibrate pressure
RsensorCalibrate:
	AA1 =(SensorCountw-ZeroCountw)/SpanValve;
	if(AA1>=5000&&AA1<=20000)		//count<40000; if>40000 then error
		{

		}
	else
		{
			goto calerror;
		}
	calpressure();
	Calshowpres();
	LCD5bitDisplayT(AA1);
calover:
	savecal();

	BUZZER_EN;
	delay(64);
	BUZZER_DIS;
	delay(128);
	goto CalOverOut;
calerror:
	DarkAllIcon();
	BUZZER_EN;
	delay(64);
	BUZZER_DIS;
	ResultCode=0x0c;
	showerror();
	goto CalOverOut;
//*************************************************************************//新增改变功能设定
ChangeFunction:
	readfunction();
	DarkAllIcon();
	for(i=1;i<=10;i++)
		{
			DisplayHalfByte(0x17,SYSHundred);							//-
			DisplayHalfByte(i,SYSTen);										//number
			DisplayHalfByte(0x17,SYSSingle);							//-
			if(i==1) BB=BPLevelType;
			if(i==2) BB=UartType;
			if(i==3) BB=KeyNumber;
			if(i==4) BB=VoiceType;
			if(i==5) BB=MemorySetType;
			if(i==6) BB=VoiceType2;
			if(i==7) BB=SystemSetFlag4.b;
			if(i==8) BB=SystemSetFlag.b;
			if(i==9) BB=SystemSetFlag2.b;
			if(i==10) BB=SystemSetFlag3.b;

			DisplayToSYSDIA(BB,2);

			do{flashParameter();}while(!KEY_TIME);
			delay(8);
			AutoOffTimew = 0;
			do
				{
					delay(1);
					flashParameter();
					if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag) goto SetParameterOver;
					if (KEY_MEMORYUP == 0)
						{
							AutoOffTimew = 0;
AddParameter:
							BB+=1;
							if (BB>ParameterArrayHigh[i-1]) BB = ParameterArrayLow[i-1];
							DisplayToSYSDIA(BB,2);
							delay(12);
							while(!KEY_MEMORYUP)
								{
									if ( AutoOffTimew > TWO)
										{
											goto AddParameter;
										}
								}
							delay(5);
						}
					if (KEY_MEMORYDOWN == 0||!KEY_POWER)
						{
							AutoOffTimew = 0;
SubParameter:
							BB-=1;
							if (BB<ParameterArrayLow[i-1]) BB = ParameterArrayHigh[i-1];
							if (BB>ParameterArrayHigh[i-1]) BB = ParameterArrayHigh[i-1];
							DisplayToSYSDIA(BB,2);
							delay(12);
							while(!KEY_MEMORYDOWN||!KEY_POWER)
								{
									if ( AutoOffTimew > TWO)
										{
											goto SubParameter;
										}
								}
								delay(5);
						}
				}while (KEY_TIME);
			while(!KEY_TIME);
			delay(1);
			while(!KEY_TIME);
			if(i==1) BPLevelType=BB;
			if(i==2) UartType=BB;
			if(i==3) KeyNumber=BB;
			if(i==4) VoiceType=BB;
			if(i==5) MemorySetType=BB;
			if(i==6) VoiceType2=BB;
			if(i==7) SystemSetFlag4.b=BB;
			if(i==8) SystemSetFlag.b=BB;
			if(i==9) SystemSetFlag2.b=BB;
			if(i==10) SystemSetFlag3.b=BB;
//			if(!KEY_POWER) goto SetParameterOver;
		}
//ParameterArrayLow[]		=	{0,	0,	1,	0,	1,		30,		0,	0,		0	};
//ParameterArrayHigh[]	=	{2,	2,	5,	3,	120,	240,	1,	255,	255};
SetParameterOver:

	savefunction();
//**************************************************
	BUZZER_EN;
	delay(64);
	BUZZER_DIS;
//***********************************************************
CalOverOut:
		while(!KEY_POWER);
		InitialLoop();
		DarkAllIcon();
		IsCALMode=0;
		IsCalibrate=0;
		IsPowerOn=0;
#endif
}


/*********************************************/
void	savetomemory(void)
{
	#if 1
	if (Year.b > 99) Year.b=0;
	ibuffer.b =  Year.b;
//	if (lastmodeAV)  ibuffer.b |= 0x80;

	checkmemorybank();	//20130609  AB user
	MemoryMode=1;

	EEPROMin=0;
	readM24L16();
	MemoryEndIndex=inbyte[0];//B=1
	MemoryEndIndex<<=8;
	MemoryEndIndex+=inbyte[1];

	if (MemoryFull)     /*inc. MemoryEndIndex */
	{
		if (MemoryEndIndex <= MemorySet)  MemoryEndIndex += 1;
		else                              MemoryEndIndex  = 2;
            	MemoryIndex = MemoryEndIndex - 1;
	}
	else
	{
		if (MemoryEndIndex  <=  MemorySet)
           	{
            		MemoryEndIndex += 1;
            		MemoryIndex = MemoryEndIndex - 1;
            		MemoryFull = 0;
			}
           	else
           	{
            		MemoryEndIndex= 2;
            		MemoryIndex =   1;
            		MemoryFull = 1;
           	}
	}
    MemoryMode=1;
	EEPROMin=0;
	readM24L16();

    outbyte[0]=MemoryEndIndex/0x100;
	outbyte[1]=MemoryEndIndex%0x100;
	outbyte[2]=inbyte[2];
	outbyte[3]=inbyte[3];
	outbyte[4]=inbyte[4];
	outbyte[5]=inbyte[5];
	if (MemoryFull) outbyte[6]=1;
	else		outbyte[6]=0;

	EEPROMin=0;
	writeM24L16();  //写index

	EEPROMin = MemoryIndex;
	outbyte[0]=ResultDataS;
	outbyte[1]=ResultDataD;
	outbyte[2]=ResultDataH;
	outbyte[3]=ResultDataDA;
	outbyte[4]=ResultDataHO;
	outbyte[5]=ResultDataMI;
	outbyte[6]=ibuffer.b;
	writeM24L16();//写数据
	MemoryMode=0;
	#endif
}


void keydelay(void)
{
	delay(15);
}
void keydelay2(void)
{
	delay(35);
}

void TimeSetThreeKey(void)
{
#if 1
	uint8 SoundCount=0;
	DarkAllIcon();
	do{flashyear();}while(KEY_TIME == 0);	//wait for key time up
	showyear();
	keydelay();
	AutoOffTimew = 0;			//initial auto exit time
	do
	{
		if(VoiceType==12)
		{
			if(SoundCount==0) {SoundNoWait(0xC1);SoundCount=1;}
			if(SoundCount==1&&Busy)
			{
				SoundCount++;
				SoundNoWait(0xC5);
			}
		}
		flashyear();
		delay(1);
		if(AutoOffTimew>AUTOOFFTIME&&!AllTimeFlag) goto SetTimeEndt;

		if (KEY_MEMORYUP == 0)
		{
			AutoOffTimew = 0;
AddYear:		Year.b+=1;
			if (Year.b>MaxYear) Year.b = YEARINIT;
		 	showyear();
		 	delay(8);
		 	while(!KEY_MEMORYUP)
		 	{

		    		if ( AutoOffTimew >TWO)
		    		{
		    			goto AddYear;
		    		}
		 	}
		 	keydelay();
		}
		if (KEY_MEMORYDOWN == 0)
		{
			AutoOffTimew = 0;
SubYear:		Year.b-=1;
			if (Year.b<YEARINIT) Year.b = MaxYear;
		 	showyear();
		 	delay(8);
		 	while(!KEY_MEMORYDOWN)
		 	{

		    		if ( AutoOffTimew >TWO)
		    		{
		    			goto SubYear;
		    		}
		 	}
		 	keydelay();
		}

	}while (KEY_POWER  && KEY_TIME);

	if (KEY_POWER == 0) goto SetTimeEndt;
	SoundCount=0;
	showyear();
	showdate();
	do{flashmonth();}while(KEY_TIME == 0);
	showdate();
	keydelay();
	AutoOffTimew = 0;
	do
	{
		if(VoiceType==12)
		{
			if(SoundCount==0) {SoundNoWait(0xC1);SoundCount=1;}
			if(SoundCount==1&&Busy)
			{
				SoundCount++;
				SoundNoWait(0xC6);
			}
		}

		showday();
		flashmonth();
		delay(1);

		if(AutoOffTimew>AUTOOFFTIME&&!AllTimeFlag) goto SetTimeEndt;

		if (KEY_MEMORYUP == 0)
		{
			AutoOffTimew = 0;
AddMonth:		Month+=1;
			if (Month>12) Month = 1;
			MDay = MonthDay[Month-1];
	     	 	if (Month ==2)
	         	{
		    		if ((YEAR0  == 0) && (YEAR1 == 0)) MDay=29;
	         	}
	         	if (Day > MDay) Day = 1;

		 	showmonth();
		 	delay(8);
		 	while(!KEY_MEMORYUP)
		 	{
		    		if ( AutoOffTimew >TWO )
		    		{
		    			goto AddMonth;
		    		}
		 	}
			keydelay();
		}
		if (KEY_MEMORYDOWN == 0)
		{
			AutoOffTimew = 0;
SubMonth:	Month-=1;
			if (Month<1) Month = 12;
			MDay = MonthDay[Month-1];
	     	 	if (Month ==2)
	         	{
		    		if ((YEAR0  == 0) && (YEAR1 == 0)) MDay=29;
	         	}
	         	if (Day > MDay) Day = 1;

		 	showmonth();
		 	delay(8);
		 	while(!KEY_MEMORYDOWN)
		 	{
		    		if ( AutoOffTimew >TWO )
		    		{
		    			goto SubMonth;
		    		}
		 	}
			keydelay();
		}


	}while (KEY_POWER  && KEY_TIME);
	if (KEY_POWER == 0) goto SetTimeEndt;

	SoundCount=0;
	showdate();
	do{flashday();}while(KEY_TIME == 0);
	showdate();
	keydelay();
	AutoOffTimew = 0;
	do
	{
		if(VoiceType==12)
		{
			if(SoundCount==0) {SoundNoWait(0xC1);SoundCount=1;}
			if(SoundCount==1&&Busy)
			{
				SoundCount++;
				SoundNoWait(0xC7);
			}
		}

		showmonth();
		flashday();
		delay(1);

		if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag) goto SetTimeEndt;

		if (KEY_MEMORYUP == 0)
		{
			AutoOffTimew = 0;
AddDay:			Day+=1;
			MDay = MonthDay[Month-1];
		 	if (Month ==2)
		   	{
		    		if (YEAR0  == 0 && YEAR1 == 0) MDay=29;
		   	}
		 	if (Day > MDay) Day = 1;

		 	showday();
		 	delay(8);
		 	while(!KEY_MEMORYUP)
		 	{
		    		if ( AutoOffTimew > TWO )
		    		{
		    			goto AddDay;
		    		}
		 	}
			keydelay();
		}
		if (KEY_MEMORYDOWN == 0)
		{
			AutoOffTimew = 0;
SubDay:		Day-=1;
			MDay = MonthDay[Month-1];
		 	if (Month ==2)
		   	{
		    		if (YEAR0  == 0 && YEAR1 == 0) MDay=29;
		   	}
		 	if (Day < 1 ) Day = MDay;

		 	showday();
		 	delay(8);
		 	while(!KEY_MEMORYDOWN)
		 	{
		    		if ( AutoOffTimew > TWO )
		    		{
		    			goto SubDay;
		    		}
		 	}
			keydelay();
		}

	}while (KEY_POWER  && KEY_TIME);

	if (KEY_POWER == 0) goto SetTimeEndt;

	SoundCount=0;
	showday();
	showtime();
	do{flashhour();}while(KEY_TIME == 0);
	showhour();
	keydelay();
	AutoOffTimew = 0;
	do
	{
		if(VoiceType==12)
		{
			if(SoundCount==0) {SoundNoWait(0xC1);SoundCount=1;}
		}

		flashhour();
		showminute();
		delay(1);

		if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag) goto SetTimeEndt;

		if (KEY_MEMORYUP == 0)
		{
			AutoOffTimew = 0;
AddHour:		Hour+=1;
		 	if (Hour>23) Hour = 0;
		 	showtime();
		 	delay(8);
		 	while(!KEY_MEMORYUP)
		 	{
		    		if ( AutoOffTimew > TWO)
		    		{
		    			goto AddHour;
		    		}
		 	}
			keydelay();
		}
		if (KEY_MEMORYDOWN == 0)
		{
			AutoOffTimew = 0;
SubHour:		Hour-=1;
		 	if (Hour>100) Hour = 23;
		 	showtime();
		 	delay(8);
		 	while(!KEY_MEMORYDOWN)
		 	{
		    		if ( AutoOffTimew > TWO)
		    		{
		    			goto SubHour;
		    		}
		 	}
			keydelay();
		}

	}while (KEY_POWER  && KEY_TIME);

	if (KEY_POWER == 0) goto SetTimeEndt;

	showtime();
	do{flashminute();}while(KEY_TIME == 0);
	showminute();
	keydelay();
	AutoOffTimew = 0;
	do
	{
		flashminute();
		showhour();
		delay(1);

		if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag) goto SetTimeEndt;

		if (KEY_MEMORYUP == 0)
		{
                       AutoOffTimew = 0;
AddMinute:		Minute+=1;
			Second = 0;
		 	if (Minute>59) Minute = 0;
		 	showtime();
		 	delay(8);
		 	while(!KEY_MEMORYUP)
		 	{

		    		if ( AutoOffTimew > TWO)
		    		{
		    			goto AddMinute;
		    		}
		 	}
			keydelay();
		}
		if (KEY_MEMORYDOWN == 0)
		{
            AutoOffTimew = 0;
SubMinute:	Minute-=1;
			Second = 0;
		 	if (Minute>100) Minute = 59;
		 	showtime();
		 	delay(8);
		 	while(!KEY_MEMORYDOWN)
		 	{

		    		if ( AutoOffTimew > TWO)
		    		{
		    			goto SubMinute;
		    		}
		 	}
			keydelay();
		}



	}while (KEY_POWER  && KEY_TIME);

	if (KEY_POWER == 0) goto SetTimeEndt;

SetTimeEndt:
	if(KeyNumber!=11) DarkAllIcon();
	if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag) IsPowerOn=0;
#endif
}
/*****************************************************/
void TimeSetTwoKey(void)
{
#if 1
	TimeBak[0]=Year.b;
	TimeBak[1]=Month;
	TimeBak[2]=Day;
	TimeBak[3]=Hour;
	TimeBak[4]=Minute;
	DarkAllIcon();
	do{flashyear();}while(KEY_TIME == 0);	//wait for key time up
	showyear();
	keydelay();
	AutoOffTimew = 0;			//initial auto exit time
	do
	{
		flashyear();
		delay(1);
		if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag) goto SetTimeEndt2;

		if (KEY_POWER == 0)
		{
			AutoOffTimew = 0;
AddYear2:		Year.b+=1;
			if (Year.b>MaxYear) Year.b = YEARINIT;
		 	showyear();
		 	delay(8);
		 	while(!KEY_POWER)
		 	{
				//if(KeyNumber==12&&LongPowerFlag) goto SetTimeEndt2;

		    		if ( AutoOffTimew >TWO)
		    		{
		    			goto AddYear2;
		    		}
		 	}
		 	keydelay();
		}

	}while (KEY_TIME);

//	if (KEY_POWER == 0) goto SetTimeEndt;

	showyear();
	showdate();
	do{flashmonth();}while(KEY_TIME == 0);
	showdate();
	keydelay();
	AutoOffTimew = 0;
	do
	{
		showday();
		flashmonth();
		delay(1);

		if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag) goto SetTimeEndt2;

		if (KEY_POWER == 0)
		{
			AutoOffTimew = 0;
AddMonth2:		Month+=1;
			if (Month>12) Month = 1;
			MDay = MonthDay[Month-1];
	     	 	if (Month ==2)
	         	{
		    		if ((YEAR0  == 0) && (YEAR1 == 0)) MDay=29;
	         	}
	         	if (Day > MDay) Day = 1;

		 	showmonth();
		 	delay(8);
		 	while(!KEY_POWER)
		 	{
				//if(KeyNumber==12&&LongPowerFlag) goto SetTimeEndt2;
		    		if ( AutoOffTimew >TWO )
		    		{
		    			goto AddMonth2;
		    		}
		 	}
			keydelay();
		}

	}while (KEY_TIME);
//	if (KEY_POWER == 0) goto SetTimeEndt;

	showdate();
	do{flashday();}while(KEY_TIME == 0);
	showdate();
	keydelay();
	AutoOffTimew = 0;
	do
	{
		showmonth();
		flashday();
		delay(1);

		if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag) goto SetTimeEndt2;

		if (KEY_POWER == 0)
		{
			AutoOffTimew = 0;
AddDay2:			Day+=1;
			MDay = MonthDay[Month-1];
		 	if (Month ==2)
		   	{
		    		if (YEAR0  == 0 && YEAR1 == 0) MDay=29;
		   	}
		 	if (Day > MDay) Day = 1;

		 	showday();
		 	delay(8);
		 	while(!KEY_POWER)
		 	{
				//if(KeyNumber==12&&LongPowerFlag) goto SetTimeEndt2;
		    		if ( AutoOffTimew > TWO )
		    		{
		    			goto AddDay2;
		    		}
		 	}
			keydelay();
		}


	}while (KEY_TIME);

//	if (KEY_POWER == 0) goto SetTimeEndt;

	showday();
	showtime();
	do{flashhour();}while(KEY_TIME == 0);
	showhour();
	keydelay();
	AutoOffTimew = 0;
	do
	{
		flashhour();
		showminute();
		delay(1);

		if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag) goto SetTimeEndt2;

		if (KEY_POWER == 0)
		{
			AutoOffTimew = 0;
AddHour2:		Hour+=1;
		 	if (Hour>23) Hour = 0;
		 	showtime();
		 	delay(8);
		 	while(!KEY_POWER)
		 	{
				//if(KeyNumber==12&&LongPowerFlag) goto SetTimeEndt2;
		    		if ( AutoOffTimew > TWO)
		    		{
		    			goto AddHour2;
		    		}
		 	}
			keydelay();
		}

	}while (KEY_TIME);

//	if (KEY_POWER == 0) goto SetTimeEndt2;

	showtime();
	do{flashminute();}while(KEY_TIME == 0);
	showminute();
	keydelay();
	AutoOffTimew = 0;
	do
	{
		flashminute();
		showhour();
		delay(1);

		if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag) goto SetTimeEndt2;

		if (KEY_POWER == 0)
		{
                       AutoOffTimew = 0;
AddMinute2:		Minute+=1;
			Second = 0;
		 	if (Minute>59) Minute = 0;
		 	showtime();
		 	delay(8);
		 	while(!KEY_POWER)
		 	{
				//if(KeyNumber==12&&LongPowerFlag) goto SetTimeEndt2;

		    		if ( AutoOffTimew > TWO)
		    		{
		    			goto AddMinute2;
		    		}
		 	}
			keydelay();
		}

	}while (KEY_TIME);

	if(KeyNumber==12)
	{
			KeyModeFlag.b=0;
			AutoOffTimew=0;
			while(KEY_POWER)
			{
				showtime();
				showdate();
				flashtimepoint();
				delay(1);
				if(AutoOffTimew > AUTOOFFTIME) break;
			}
	}
SetTimeEndt2:
	if(KeyNumber!=12) DarkAllIcon();
	/*if(KeyNumber==12&&LongPowerFlag)
	{
		Year.b=TimeBak[0];
		Month=TimeBak[1];
		Day=TimeBak[2];
		Hour=TimeBak[3];
		Minute=TimeBak[4];
	}*/
	if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag) IsPowerOn=0;
#endif
}



void	In_Deflate(void)
{

	unsigned char	i;
	Mathlw=0;
	Mathhw=0;
	for(i=0;i<5;i++)
		{
			Mathlw+=LeakageP[i];
		}
	for(i=5;i<10;i++)
 		{
			Mathhw+=LeakageP[i];
		}
	if(Mathhw>=Mathlw)
		{
			InflateValue=(Mathhw-Mathlw)/5;
			DeflateValue=0;
		}
	else
		{
			InflateValue=0;
			DeflateValue=(Mathlw-Mathhw)/5;
		}
	if(InflateValue>255) InflateValue=255;    //20120717
	if(PressuremmHgw<limit) InflateValue=210;
//	if(DeflateValue<60) DeflateValue=60;    //20120717

}

void	checkpressure(void)
{

	if (PressuremmHgw > MAX_PRESSURE)	  	// if pressure > 300 mmHg then
		{
			BB= VALVEOFF | PUMPOFF;	  		//off valve,lbt,pump I/O
			iocontroloff();
			DarkAllIcon();						//dark all pattern on LCD
			DisplayHalfByte(0x11,DIATen);        //H'
			DisplayHalfByte(0x12,DIASingle);     //I'
//ALARM_HI:
			N=0;
			do
				{
					BUZZER_EN;				// buzzer long 3 seconds beep 128
					delay(16);
					BUZZER_DIS;
					delay(8);
					N++;
				}while(N<3&&KEY_POWER);
			IsAlgorithm = 0;
			IsAlgoEnd = 1;					//set algorithm end flag = 1
			ResultCode=3;
			IsShowPressure=0;
			delay(64);
			goto exitCHECK_HI;
		}

	if (!IsAlgorithm)
		{
			if (PressuremmHgw<16)
				{
					//OUT_VALVE=1;
					RePump=0;
					IsPump=1;
        	//SetPressureVw=160;
        	//IsShowResult = 0;
					//IsShowPressure = 1;
					//IsAlgorithm = 0;
					//IsAlgoEnd = 0;
				}
			if (IsShowPressure==0)
				{
					DarkAllIcon();
					//showtime();
					IsShowPressure = 1;
				}

			if (PressuremmHgw > SetPressureVw)
				{
					AutoOffTimew=0x0000;
					BB=PUMPOFF;
					iocontroloff();
					IsPump=0;
					IsShowPressure = 1;
					IsAlgorithm = 1;
					PriPressureV=PressuremmHgw;
//    		deflate_en=0;
					algorithminit();
					NormalSignal=1;
					SmallSignalOn=0;
					RePump=0;
	 			}
		}
	else
		{
			if (PressuremmHgw>SetPressureVw)
				{
					AutoOffTimew=0x0000;
					SetPressureVw=PressuremmHgw;
					IsPump=0;
					IsAlgoEnd=0;
					IsAlgorithm = 1;		/* set algorithm = 1 */
					algorithminit();	//20100404
					NormalSignal=1;
					SmallSignalOn=0;
					RePump=0; 	  /*!!!!!!*/
				}
		}
exitCHECK_HI:
	nop4();
	//_nop_();

}






void LifeTest(void)
{
	unsigned int LifeTime=0;
	//unsigned int LifeTimeH=0;
	PUMPPWMValue=65530;
	DarkAllIcon();
	while(!KEY_POWER);
	delay(8);
	do
		{
			DisplayHalfByte(LifeTime/10000,DayTen);
			DisplayHalfByte((LifeTime%10000)/1000,DaySingle);
			DisplayHalfByte((LifeTime%1000)/100,HourSingle);
			DisplayHalfByte((LifeTime%100)/10,MINTen);
			DisplayHalfByte(LifeTime%10,MINSingle);
			LifeTime++;
			zerotracing();
			BB=PUMP|VALVE;
			PWM_SetDutyCycle(PWM1,255);//ON PUMP
			iocontrolon();
			do{
					delay(1);
					calpressure();
					if (T125ms == 0) OneShotDC = 0;
					else
						{
							if (OneShotDC == 0)
								{
									OneShotDC = 1;
									DisplayToSYSDIA(PressuremmHgw/10,2);
								}
						}
					CheckBatteryCOM();
				}while((PressuremmHgw<3000)&&(KEY_POWER));
/*
			BB=PUMPOFF;
			iocontroloff();
			do
				{
					calpressure();
					if (T8Hz == 0) OneShotDC = 0;
					else
						{
							if (OneShotDC == 0)
								{
									OneShotDC = 1;
									DisplayToSYSDIA(PressuremmHgw/10,2);
								}
						}
					CheckBatteryCOM();
				}while((PressuremmHgw>500)&&(KEY_POWER));
*/
			BB=PUMPOFF|VALVEOFF;
			iocontroloff();
			AutoOffTimew=0;
			do
				{
					delay(1);
					calpressure();
					if (T125ms == 0) OneShotDC = 0;
					else
						{
							if (OneShotDC == 0)
								{
									OneShotDC = 1;
									DisplayToSYSDIA(PressuremmHgw/10,2);
								}
						}
					CheckBatteryCOM();
				}while(AutoOffTimew<344&&(KEY_POWER));   //8S
			}while(KEY_POWER&&IsPowerOn&&(LifeTime<=10000));
	while(KEY_POWER);
	IsPowerOn=0;
}



void TestValve(void)
{
//	uint8 ValveMode=0;
	DarkAllIcon();
	VavleTestFlag=1;
	CheckBatteryADC();
	zerotracing();
	algorithminit();
	IsCalibrate=0;
	SetPressureVw=280;

	DisplayHalfByte(BatValue/100,HourSingle);
	DisplayHalfByte(BatValue%100/10,MINTen);
	DisplayHalfByte(BatValue%10,MINSingle);


	while(KEY_POWER)
	{
		if(!KEY_TIME)
		{
			BB=VALVEOFF;
			iocontroloff();
			showvalveoffset();
			while(!KEY_TIME);
			keydelay();
			while(KEY_POWER)
			{
				if(!KEY_TIME)
				{
					if(valveoffset<800) valveoffset+=50;
					else valveoffset=0;
					showvalveoffset();
					while(!KEY_TIME);
					keydelay();
				}
			}
			EEPROMin=valveoffsetAdd;
			outbyte[0]=valveoffset/0x100;
			outbyte[1]=valveoffset%0x100;
			outbyte[6]=0xac;
			writeM24L16();
			while(!KEY_POWER);
			keydelay();
			DisplayHalfByte(0x10,SYSHundred);
			DisplayHalfByte(0x10,SYSTen);
			DisplayHalfByte(0x10,SYSSingle);
		}

		if(!KEY_MEMORYUP)
		{
			BB=VALVE;
			iocontrolon();
			PumpUp(238);
			PUMPPWM_EN;
			do
			{
				ADOKFlag=0;
				while(!ADOKFlag);
				calpressure();
				showpressure();
			}
			while(PressuremmHgw<SetPressureVw&&KEY_POWER);
			BB=PUMPOFF;
			iocontroloff();

			first1flag=0;
			starttime=0;
			rate180=rate120=rate80=rate60=199;
			flag180=flag120=flag80=flag60=0;
			do
			{
				ADOKFlag=0;
				while(!ADOKFlag);
				calpressure();
				showpressure();
				In_Deflate();
				ValvePwm();
				Send_filter_test();
				DisplayToSYSDIA(DeflateValue,1);

				if(PressuremmHgw>SetPressureVw-40)
				{
					starttime++;
				}
				if(PressuremmHgw<=180&&flag180==0)
				{
					flag180=1;
					rate180=DeflateValue;
				}
				if(PressuremmHgw<=120&&flag120==0)
				{
					flag120=1;
					rate120=DeflateValue;
				}
				if(PressuremmHgw<=80&&flag80==0)
				{
					flag80=1;
					rate80=DeflateValue;
				}
				if(PressuremmHgw<=60&&flag60==0)
				{
					flag60=1;
					rate60=DeflateValue;
				}
			}
			while(flag60==0&&KEY_POWER);

			BB=PUMPOFF|VALVEOFF;
			iocontroloff();

			Mathhw=20000/starttime;
			if(Mathhw>199) Mathhw=199;
			starttime=Mathhw;

			DisplayHalfByte(starttime/100,MonthSingle);
			DisplayHalfByte(starttime%100/10,DayTen);
			DisplayHalfByte(starttime%10,DaySingle);

			DisplayToSYSDIA(rate180,1);
			DisplayToSYSDIA(rate120,2);
			DisplayToMemorySet(rate80);
			DisplayToPUL(rate60);

			while(!KEY_POWER);
			keydelay();
		}
	}
			BB=PUMPOFF|VALVEOFF;
			iocontroloff();
			VavleTestFlag=0;
}


void Key11Clr(void)
{
	DarkAllIcon();
	LoadMemory();
	DisplayHalfByte(0xC,DIAHundred);
	DisplayHalfByte(0x13,DIATen);
	DisplayHalfByte(0xb,DIASingle);

	while(!KEY_TIME) flashuser();
	keydelay();
	AutoOffTimew=0;
	while(KEY_TIME)
	{
		delay(1);
		flashuser();
		if(!KEY_MEMORYUP||!KEY_MEMORYDOWN)
		{
			if(UserA) UserA=0;
			else UserA=1;
			UserABDisplay();
			LoadMemory();
			while(!KEY_MEMORYUP||!KEY_MEMORYDOWN);
			keydelay();
		}
		if(!KEY_POWER||AutoOffTimew>AUTOOFFTIME) goto EXIT;
	}
	UserABDisplay();
	while(!KEY_TIME) flashmemoryset(MemoryIndex);
	keydelay();
	AutoOffTimew=0;
	while(!LongSetFlag)
	{
		delay(1);
		flashmemoryset(MemoryIndex);
		if(!KEY_POWER||AutoOffTimew>AUTOOFFTIME) goto EXIT;
	}
	CLRMemoryBank();
	DisplayToMemorySet(0);

	while(!KEY_TIME);
	keydelay();
	AutoOffTimew=0;
	while(!LongSetFlag)
	{
		delay(1);
		if(!KEY_POWER||AutoOffTimew>AUTOOFFTIME) goto EXIT;
	}

	if(UserA) UserA=0;
	else UserA=1;
	UserABDisplay();
	LoadMemory();

	while(!KEY_TIME) flashmemoryset(MemoryIndex);
	keydelay();
	AutoOffTimew=0;
	while(!LongSetFlag)
	{
		delay(1);
		flashmemoryset(MemoryIndex);
		if(!KEY_POWER||AutoOffTimew>AUTOOFFTIME) goto EXIT;
	}
	CLRMemoryBank();
	DisplayToMemorySet(0);
	AutoOffTimew=0;
	while(KEY_POWER)
	{
		delay(1);
		if(AutoOffTimew>AUTOOFFTIME) goto EXIT;
	}
EXIT:
	MemoryMode=0;
}

void Key12Clr(void)
{
	DarkAllIcon();
	LoadMemory();
	DisplayHalfByte(0xC,DIAHundred);
	DisplayHalfByte(0x13,DIATen);
	DisplayHalfByte(0xb,DIASingle);

	while(!KEY_TIME) flashuser();
	keydelay();
	AutoOffTimew=0;
	while(KEY_TIME)
	{
		delay(1);
		flashuser();
		if(!KEY_POWER)
		{
			if(UserA) UserA=0;
			else UserA=1;
			UserABDisplay();
			LoadMemory();
			while(!KEY_POWER)
			{
				//if(LongPowerFlag) goto EXIT; //20200409删除
			}
			keydelay();
		}
		if(AutoOffTimew>AUTOOFFTIME) goto EXIT;
	}
	UserABDisplay();
	while(!KEY_TIME) flashmemoryset(MemoryIndex);
	keydelay();
	AutoOffTimew=0;
	while(!LongSetFlag)
	{
		delay(1);
		flashmemoryset(MemoryIndex);
		if(/*LongPowerFlag||*/AutoOffTimew>AUTOOFFTIME) goto EXIT;
	}
	CLRMemoryBank();
	UserABDisplay();
	DisplayHalfByte(0xC,DIAHundred);
	DisplayHalfByte(0x13,DIATen);
	DisplayHalfByte(0xb,DIASingle);
	DisplayToMemorySet(0);

	while(!KEY_TIME);
	keydelay();
	AutoOffTimew=0;
	while(!LongSetFlag)
	{
		delay(1);
		if(!KEY_POWER||AutoOffTimew>AUTOOFFTIME) goto EXIT;
	}

	if(UserA) UserA=0;
	else UserA=1;
	UserABDisplay();
	LoadMemory();

	while(!KEY_TIME) flashmemoryset(MemoryIndex);
	keydelay();
	AutoOffTimew=0;
	while(!LongSetFlag)
	{
		delay(1);
		flashmemoryset(MemoryIndex);
		if(/*LongPowerFlag||*/AutoOffTimew>AUTOOFFTIME) goto EXIT;
	}
	CLRMemoryBank();
	UserABDisplay();
	DisplayHalfByte(0xC,DIAHundred);
	DisplayHalfByte(0x13,DIATen);
	DisplayHalfByte(0xb,DIASingle);
	DisplayToMemorySet(0);
	AutoOffTimew=0;
	while(KEY_POWER)
	{
		delay(1);
		if(AutoOffTimew>AUTOOFFTIME) goto EXIT;
	}
EXIT:
	MemoryMode=0;
}



void totimekeyin(void)
{
//	unsigned char i=0;
	IsPowerOn = 1;
	AutoOffTimew = 0;
	UserBak=UserA;
	if(KeyNumber==7) goto USERSET;

UNITSET:

	if(UnitSetEnable&&!SetMode2)
	{
		DarkAllIcon();
		mmHgKpaSetDisplay();
		while(!KEY_TIME);
		keydelay();
		if(VoiceType==12) SoundNoWait(0xC3);
	do									//设定mmHg和Kpa单位
		{
			mmHgKpaSetDisplay();
			AutoOffTimew = 0;
			do{
				delay(1);
				if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag){goto ToTimeOver;}
			  }while(KEY_MEMORYDOWN&& KEY_MEMORYUP&& KEY_TIME && KEY_POWER);

			if(ThreeKeyFlag)
				{
					if(!KEY_MEMORYDOWN || !KEY_MEMORYUP)
						{
							if(mmHgkPaFlag==1)
								{
									mmHgkPaFlag=0;
								}
							else{mmHgkPaFlag=1;}
							mmHgKpaSetDisplay();
							while(!KEY_MEMORYDOWN || !KEY_MEMORYUP);
							keydelay();
							if(mmHgkPaFlag==0)
							{
								if(VoiceType==12) SoundNoWait(0xE3);
							}
						}
					if(!KEY_POWER) {IsPowerOn=0; goto ToTimeOver;}
				}
			else
				{
					if(!KEY_POWER)
						{
							if(mmHgkPaFlag==1)
								{
									mmHgkPaFlag=0;
								}
							else{mmHgkPaFlag=1;}
							mmHgKpaSetDisplay();
							while(!KEY_POWER)
							{
								//if(KeyNumber==12&&LongPowerFlag) goto ToTimeOver;
							}
							keydelay();
							if(mmHgkPaFlag==0)
							{
								if(VoiceType==12) SoundNoWait(0xE3);
							}
						}
				}
		}while(KEY_TIME);

		while(!KEY_TIME);
		keydelay();
	}

	if(VoiceType==4||VoiceType==11)	//英土双语
	{
		EngTurDisplay();
		while(!KEY_TIME);
		keydelay();

		do									//设定语言
			{
				EngTurDisplay();
				AutoOffTimew = 0;
				do{
					delay(1);
					if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag){goto ToTimeOver;}
					}while(KEY_MEMORYDOWN&& KEY_MEMORYUP&& KEY_TIME && KEY_POWER);

					if(ThreeKeyFlag)
					{
						if(!KEY_MEMORYDOWN || !KEY_MEMORYUP)
							{
								if(VoiceFlag==1)
									{
										VoiceFlag=0;
									}
								else{VoiceFlag=1;}
								EngTurDisplay();
								if(VoiceType==11)
								{
									VoiceSetFlag=1;
									if(VoiceFlag==0) SoundAllCommand2(0x22,0,0,0);
									else SoundAllCommand2(0x11,0,0,0);
									VoiceSetFlag=0;
								}
								while(!KEY_MEMORYDOWN || !KEY_MEMORYUP);
								keydelay();
							}
						if(!KEY_POWER) goto ToTimeOver;
					}
					else
					{
						if(!KEY_POWER)
							{
								if(VoiceFlag==1)
									{
										VoiceFlag=0;
									}
								else{VoiceFlag=1;}
								EngTurDisplay();
								if(VoiceType==11)
								{
									VoiceSetFlag=1;
									if(VoiceFlag==0) SoundAllCommand2(0x22,0,0,0);
									else SoundAllCommand2(0x11,0,0,0);
									VoiceSetFlag=0;
								}
								while(!KEY_POWER)
								{
									//if(KeyNumber==12&&LongPowerFlag) goto ToTimeOver;
								}
								keydelay();
							}
					}

			}while(KEY_TIME);

		while(!KEY_TIME);
		keydelay();
	}

	if(VoiceType==8)	//五国语音
	{
		DisplayHalfByte(VoiceFlag+1,DIASingle);
		while(!KEY_TIME);
		keydelay();

		do									//设定语言
			{
				DisplayHalfByte(VoiceFlag+1,DIASingle);
				AutoOffTimew = 0;
				do{
					delay(1);
					if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag){goto ToTimeOver;}
					}while(KEY_MEMORYDOWN&& KEY_MEMORYUP&& KEY_TIME && KEY_POWER);

					if(ThreeKeyFlag)
					{
						if(!KEY_MEMORYUP)
							{
								VoiceSetFlag=1;
								VoiceFlag++;
								if(VoiceFlag>5)
									{
										VoiceFlag=0;
									}
								DisplayHalfByte(VoiceFlag+1,DIASingle);
								SoundFive();
								while(!KEY_MEMORYUP);
								keydelay();
								VoiceSetFlag=0;
							}
						if(!KEY_MEMORYDOWN)
						{
								VoiceSetFlag=1;
								VoiceFlag--;
								if(VoiceFlag>5)
									{
										VoiceFlag=5;
									}
								DisplayHalfByte(VoiceFlag+1,DIASingle);
								SoundFive();
								while(!KEY_MEMORYDOWN);
								keydelay();
								VoiceSetFlag=0;
						}
						if(!KEY_POWER) goto ToTimeOver;
					}
					else
					{
						if(!KEY_POWER)
							{
								VoiceSetFlag=1;
								VoiceFlag++;
								if(VoiceFlag>5)
									{
										VoiceFlag=0;
									}
								DisplayHalfByte(VoiceFlag+1,DIASingle);
								SoundFive();
								while(!KEY_POWER)
								{
									//if(KeyNumber==12&&LongPowerFlag) goto ToTimeOver;
								}
								keydelay();
								VoiceSetFlag=0;
							}
					}

			}while(KEY_TIME);

		while(!KEY_TIME);
		keydelay();
	}


	if(VoiceType==1&&VoiceType2==3)	//中英马来西亚 设置
	{
		DarkAllIcon();
        swit_disp_china_eng_malay(e_voice_c_e_ma_switch);//中语英语马来西亚语切换和显示
		while(!KEY_TIME);
		keydelay();

		do									//设定语言
			{
                swit_disp_china_eng_malay(e_voice_c_e_ma_switch);//中语英语马来西亚语切换和显示
				AutoOffTimew = 0;
				do{
					delay(1);
					if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag){goto ToTimeOver;}
					}while(KEY_MEMORYDOWN&& KEY_MEMORYUP&& KEY_TIME && KEY_POWER);

					if(ThreeKeyFlag)
					{
						if(!KEY_MEMORYUP)
							{
								VoiceSetFlag=1;
								e_voice_c_e_ma_switch++;
								if(e_voice_c_e_ma_switch>=E_VOICE_MAX)
                                {
                                    e_voice_c_e_ma_switch=0;
                                }
                                swit_disp_china_eng_malay(e_voice_c_e_ma_switch);//中语英语马来西亚语切换和显示
								while(!KEY_MEMORYUP);
								keydelay();
								VoiceSetFlag=0;
							}
						if(!KEY_MEMORYDOWN)
						{
								VoiceSetFlag=1;
								e_voice_c_e_ma_switch--;
								if(e_voice_c_e_ma_switch<=0)
                                {
                                    e_voice_c_e_ma_switch=E_VOICE_MAX;
                                }
                                swit_disp_china_eng_malay(e_voice_c_e_ma_switch);//中语英语马来西亚语切换和显示
								while(!KEY_MEMORYDOWN);
								keydelay();
								VoiceSetFlag=0;
						}
						if(!KEY_POWER) goto ToTimeOver;
					}
					else
					{
						if(!KEY_POWER)
							{
								VoiceSetFlag=1;
								e_voice_c_e_ma_switch++;
								if(e_voice_c_e_ma_switch>=E_VOICE_MAX)
                                {
                                    e_voice_c_e_ma_switch=0;
                                }
                                swit_disp_china_eng_malay(e_voice_c_e_ma_switch);//中语英语马来西亚语切换和显示
								while(!KEY_POWER)
								{
									//if(KeyNumber==12&&LongPowerFlag) goto ToTimeOver;
								}
								keydelay();
								VoiceSetFlag=0;
							}
					}

			}while(KEY_TIME);

		while(!KEY_TIME);
		keydelay();
	}

	if(VoiceType==1&&VoiceType2==3)	//中英马来西亚调音量
	{
		DarkAllIcon();
        volume_disp_china_eng_malay(VoiceLevel);//中语英语马来西亚语音量调节
        volume_adjus_china_eng_malay(VoiceLevel);
		while(!KEY_TIME);
		keydelay();

		do
			{
				AutoOffTimew = 0;
				do{
					delay(1);
					if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag){goto ToTimeOver;}
					}while(KEY_MEMORYDOWN&& KEY_MEMORYUP&& KEY_TIME && KEY_POWER);

					if(ThreeKeyFlag)
					{
						if(!KEY_MEMORYUP)
							{
								VoiceSetFlag=1;
								VoiceLevel++;
								if(VoiceLevel>3)
								{
									VoiceLevel=0;
								}
                                volume_disp_china_eng_malay(VoiceLevel);
                                volume_adjus_china_eng_malay(VoiceLevel);

								while(!KEY_MEMORYUP);
								keydelay();
								VoiceSetFlag=0;
							}
						if(!KEY_MEMORYDOWN)
						{
								VoiceSetFlag=1;
								VoiceLevel--;
								if(VoiceLevel>3)
									{
										VoiceLevel=3;
									}
                                volume_disp_china_eng_malay(VoiceLevel);
                                volume_adjus_china_eng_malay(VoiceLevel);
								while(!KEY_MEMORYDOWN);
								keydelay();
								VoiceSetFlag=0;
						}
						if(!KEY_POWER) goto ToTimeOver;
					}
					else
					{
						if(!KEY_POWER)
							{
								VoiceSetFlag=1;
								VoiceLevel++;
								if(VoiceLevel>3)
								{
									VoiceLevel=0;
								}
                                volume_disp_china_eng_malay(VoiceLevel);
                                volume_adjus_china_eng_malay(VoiceLevel);
								while(!KEY_POWER)
								{
									//if(KeyNumber==12&&LongPowerFlag) goto ToTimeOver;
								}
								keydelay();
								VoiceSetFlag=0;
							}
					}

			}while(KEY_TIME);

		while(!KEY_TIME);
		keydelay();
	}


	if(VoiceType==13&&VoiceType2>1)	//中英泰
	{
		DarkAllIcon();
		ShowVoiceCET();
		while(!KEY_TIME);
		keydelay();

		do									//设定语言
			{
				ShowVoiceCET();
				AutoOffTimew = 0;
				do{
					delay(1);
					if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag){goto ToTimeOver;}
					}while(KEY_MEMORYDOWN&& KEY_MEMORYUP&& KEY_TIME && KEY_POWER);

					if(ThreeKeyFlag)
					{
						if(!KEY_MEMORYUP)
							{
								VoiceSetFlag=1;
								VoiceFlag--;
								if(VoiceFlag>2)
									{
										VoiceFlag=2;
									}
								ShowVoiceCET();
								SoundThree();
								while(!KEY_MEMORYUP);
								keydelay();
								VoiceSetFlag=0;
							}
						if(!KEY_MEMORYDOWN)
						{
								VoiceSetFlag=1;
								VoiceFlag++;
								if(VoiceFlag>2)
									{
										VoiceFlag=0;
									}
								ShowVoiceCET();
								SoundThree();
								while(!KEY_MEMORYDOWN);
								keydelay();
								VoiceSetFlag=0;
						}
						if(!KEY_POWER) goto ToTimeOver;
					}
					else
					{
						if(!KEY_POWER)
							{
								VoiceSetFlag=1;
								VoiceFlag--;
								if(VoiceFlag>2)
									{
										VoiceFlag=2;
									}
								ShowVoiceCET();
								SoundThree();
								while(!KEY_POWER)
								{
									//if(KeyNumber==12&&LongPowerFlag) goto ToTimeOver;
								}
								keydelay();
								VoiceSetFlag=0;
							}
					}

			}while(KEY_TIME);

		while(!KEY_TIME);
		keydelay();
	}

	if(VoiceType==13&&(VoiceType2==1||VoiceType2==3))	//调音量
	{
		DarkAllIcon();
		ShowVoiceLevel();
		while(!KEY_TIME);
		keydelay();

		do
			{
				ShowVoiceLevel();
				AutoOffTimew = 0;
				do{
					delay(1);
					if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag){goto ToTimeOver;}
					}while(KEY_MEMORYDOWN&& KEY_MEMORYUP&& KEY_TIME && KEY_POWER);

					if(ThreeKeyFlag)
					{
						if(!KEY_MEMORYUP)
							{
								VoiceSetFlag=1;
								VoiceLevel++;
								if(VoiceLevel>3)
									{
										VoiceLevel=0;
									}
								ShowVoiceLevel();
								SoundLevelSet();
								while(!KEY_MEMORYUP);
								keydelay();
								VoiceSetFlag=0;
							}
						if(!KEY_MEMORYDOWN)
						{
								VoiceSetFlag=1;
								VoiceLevel--;
								if(VoiceLevel>3)
									{
										VoiceLevel=3;
									}
								ShowVoiceLevel();
								SoundLevelSet();
								while(!KEY_MEMORYDOWN);
								keydelay();
								VoiceSetFlag=0;
						}
						if(!KEY_POWER) goto ToTimeOver;
					}
					else
					{
						if(!KEY_POWER)
							{
								VoiceSetFlag=1;
								VoiceLevel++;
								if(VoiceLevel>3)
								{
									VoiceLevel=0;
								}
								ShowVoiceLevel();
								SoundLevelSet();
								while(!KEY_POWER)
								{
									//if(KeyNumber==12&&LongPowerFlag) goto ToTimeOver;
								}
								keydelay();
								VoiceSetFlag=0;
							}
					}

			}while(KEY_TIME);

		while(!KEY_TIME);
		keydelay();
	}

	if(KeyNumber==7) goto NEXT;
USERSET:

	if(TwoUser==1&&UserSetEnable)
	{
		DarkAllIcon();
		UserABDisplay();
		while(!KEY_TIME)
		{
			if(KeyNumber==11||KeyNumber==12||UartType==6) flashuser();
		}
		keydelay();

		do								//设定UserA及userB
		{
			if(KeyNumber!=12&&KeyNumber!=11&&UartType!=6)
			{
				DarkAllIcon();
				UserABDisplay();
			}
			AutoOffTimew = 0;
			do{
					if(KeyNumber==12||KeyNumber==11||UartType==6)
					{
						flashuser();
					}
					delay(1);
					if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag){goto ToTimeOver;}
			  }
			while(KEY_MEMORYDOWN&& KEY_MEMORYUP&& KEY_TIME && KEY_POWER);

			if(ThreeKeyFlag)
				{
					if(!KEY_MEMORYDOWN || !KEY_MEMORYUP)
						{
							if(UserA==1)
								{
									UserA=0;
								}
							else{UserA=1;}
							DarkAllIcon();
							UserABDisplay();
							while(!KEY_MEMORYDOWN || !KEY_MEMORYUP);
							keydelay();
						}
					if(!KEY_POWER) {IsPowerOn=0; goto ToTimeOver;}
				}
			else
				{
					if(!KEY_POWER)
						{
							if(UserA==1)
								{
									UserA=0;
								}
							else{UserA=1;}
							DarkAllIcon();
							UserABDisplay();
							while(!KEY_POWER)
							{
								//if(KeyNumber==12&&LongPowerFlag) goto ToTimeOver;
							}
							keydelay();
						}
				}
		}while(KEY_TIME);

		while(!KEY_TIME);
		keydelay();
	}

	if(KeyNumber==7) goto UNITSET;

TIMESET:

//	if(VoiceType==6)
	if((VoiceType==6) || ((VoiceType==1) && (VoiceType2==2)))	//20230508 modify
	{
		DarkAllIcon();
		SPDisplay();
		while(!KEY_TIME);
		keydelay();

		do									//设定普通话粤语
		{
			SPDisplay();
			AutoOffTimew = 0;
			do{
				delay(1);
				if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag){goto ToTimeOver;}
				}while(KEY_MEMORYUP&&KEY_MEMORYDOWN&& KEY_TIME && KEY_POWER);

				if(ThreeKeyFlag)
				{
					if(!KEY_MEMORYUP||!KEY_MEMORYDOWN )
					{
						VoiceSetFlag=1;
						if(VoiceFlag==1)
						{
							VoiceFlag=0;
							if((VoiceType==1) && (VoiceType2==2))	//20230508
							{
								SoundAllCommand(0x11,0,0,0);	//CN
							}
							else
							{
								SoundAllCommand2(0x22,0,0,0);
							}
						}
						else
						{
							VoiceFlag=1;
							if((VoiceType==1) && (VoiceType2==2))	//20230508
							{
								SoundAllCommand(0x22,0,0,0);	//MD
							}
							else
							{
								SoundAllCommand2(0x33,0,0,0);
							}
						}
						SPDisplay();
						delay(4);

						if((VoiceType==1) && (VoiceType2==2))
						{

						}
						else
						{
							SoundCommand2(0xE0);
						}

						while(!KEY_MEMORYUP||!KEY_MEMORYDOWN );
						keydelay();
						VoiceSetFlag=0;
					}
					if(!KEY_POWER) goto ToTimeOver;
				}
				else
				{
					if(!KEY_POWER)
					{
						VoiceSetFlag=1;
						if(VoiceFlag==1)
						{
							VoiceFlag=0;
							if((VoiceType==1) && (VoiceType2==2))
							{
								SoundAllCommand2(0x11,0,0,0);
							}
							else
							{
								SoundAllCommand2(0x22,0,0,0);
							}
						}
						else
						{
							VoiceFlag=1;
							if((VoiceType==1) && (VoiceType2==2))
							{
								SoundAllCommand2(0x22,0,0,0);
							}
							else
							{
								SoundAllCommand2(0x33,0,0,0);
							}
						}
						SPDisplay();
						delay(4);
						SoundCommand2(0xE0);
						while(!KEY_POWER)
						{
							//if(KeyNumber==12&&LongPowerFlag) goto ToTimeOver;
						}
						keydelay();
						VoiceSetFlag=0;
					}
				}

		}while(KEY_TIME);

		while(!KEY_TIME);
		keydelay();
	}

	if(AlarmClock)
	{
		AlarmSet();
		if(!IsPowerOn||!KEY_POWER) goto ToTimeOver;
	}

		if(!NoTimer && IsPowerOn)
			{
				while(!KEY_TIME);
				keydelay();
				if(ThreeKeyFlag) TimeSetThreeKey();
				else TimeSetTwoKey();

				if(KEY_POWER==0||!IsPowerOn){goto ToTimeOver;}
				while(!KEY_TIME);
				keydelay();
			}

		if(KeyNumber==7) goto NEXT2;
NEXT:

		if(VoiceSetEnable)
		{
			SpeakDisplay();
			while(!KEY_TIME);
			keydelay();
			if(VoiceType==12) SoundNoWait(0xC2);
			do
				{
				SpeakDisplay();
				AutoOffTimew = 0;
				do{
					delay(1);
					if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag){goto ToTimeOver;}
					}
				while(KEY_MEMORYDOWN&& KEY_MEMORYUP&& KEY_TIME && KEY_POWER);

				if(ThreeKeyFlag)
					{
						if(!KEY_MEMORYDOWN || !KEY_MEMORYUP)
							{
								if(VoiceEnable==1)
									{
										VoiceEnable=0;
									}
								else{VoiceEnable=1;}
								SpeakDisplay();
								while(!KEY_MEMORYDOWN || !KEY_MEMORYUP);
								keydelay();
							}
						if(!KEY_POWER) {IsPowerOn=0; goto ToTimeOver;}
					}
				else
					{
						if(!KEY_POWER)
							{
								if(VoiceEnable==1)
									{
										VoiceEnable=0;
									}
								else{VoiceEnable=1;}
								DarkAllIcon();
								SpeakDisplay();
								while(!KEY_POWER)
								{
									//if(KeyNumber==12&&LongPowerFlag) goto ToTimeOver;
								}
								keydelay();
							}
					}
				}while(KEY_TIME);

			while(!KEY_TIME);
			keydelay();
		}

		if(KeyNumber==7) goto TIMESET;
//************************************************新增记忆删除功能
NEXT2:
		if(KeyNumber!=11&&KeyNumber!=12)
		{
			DarkAllIcon();
			AutoOffTimew=0;
			do{
				DisplayHalfByte(0x0c,DIATen);		// 'C'
					DisplayHalfByte(0x13,DIASingle);	// 'L'
					if(AutoOffTimew > AUTOOFFTIME&&!AllTimeFlag){goto ToTimeOver;}
					delay(1);
					}while(KEY_POWER&&KEY_TIME);
			if(!KEY_POWER)
				{
					CLRMemoryBank();
					flashCL();		//CL flash
				}
			}
		else
		{
			if(KeyNumber==12) goto ToTimeOver;
			KeyModeFlag.b=0;
			AutoOffTimew=0;
			while(KEY_POWER&&IsPowerOn)
			{
				if(!KEY_MEMORYUP||!KEY_MEMORYDOWN) {MemoryKeyFlag=1;break;}
				if(LongSetFlag) {SetKeyFlag=1;break;}
				showtime();
				showdate();
				flashtimepoint();
				delay(1);
				if(AutoOffTimew > AUTOOFFTIME) break;
			}
		}
ToTimeOver:
		IsPowerOn=0;
		//if(KeyNumber==12&&LongPowerFlag) UserA=UserBak;
		SaveSet();
		if(KeyNumber==12) DarkAllIcon();
		if(VoiceType==12)
		{
			while(!KEY_POWER||!KEY_TIME);
			keydelay();
			SoundCommand(0xC4);
		}
		DarkAllIcon();
}



void unitsetonekey(void)
{
	AutoOffTimew = 0;
	DarkAllIcon();
	mmHgKpaSetDisplay();
	while(!KEY_POWER&&(AutoOffTimew<200));
	if(AutoOffTimew>199) return;
	do									//设定mmHg和Kpa单位
		{
			mmHgKpaSetDisplay();
			AutoOffTimew = 0;
			do{
				delay(1);
				if(AutoOffTimew > 1499) goto SaveSetValue;
			  }while( KEY_POWER);


					if(!KEY_POWER)
						{
							AutoOffTimew=0;
							while(!KEY_POWER&&(AutoOffTimew<150));
							if(AutoOffTimew>149)
							{
								goto SaveSetValue;
							}
							if(mmHgkPaFlag==1)
								{
									mmHgkPaFlag=0;
								}
							else{mmHgkPaFlag=1;}
							delay(4);
						}
		}while(AutoOffTimew<1500);
SaveSetValue:
		DarkAllIcon();
		SaveSet();
		while(!KEY_POWER);
		delay(4);
}


void	deflate(void)
{
	if(IsCalibrate)										// CA mode deflate rate
		{
			if(SecondFlag == 1)
				{
					SecondFlag = 0;
					if(DiaMeanEnd > SysMeanEnd)
						{
							Mathlw = DiaMeanEnd - SysMeanEnd;
						}
					else  Mathlw=0;
					if(Mathlw > 199 ) Math01b = 199;
					if(PressuremmHgw < 6)
						{
							OneSecDeflated=0;
						}
					else	OneSecDeflated=Math01b;
					DisplayToPUL(OneSecDeflated);
					DiaMeanEnd = SysMeanEnd;
     		}
		}
}


void SoundWHO(uint8 SYS,uint8 DIA)
{
	VoiceStop=0;
	if(SYS<80)
	{
		SoundCommand(0xE1);
		if(VoiceStop) return;
		SoundCommand(0xFD);//偏低
	}
	else if(SYS>=WHOSYS2)
	{
		SoundCommand(0xE1);
		if(VoiceStop) return;
		SoundCommand(0xFC);
	}
	if(VoiceStop) return;
	if(DIA<50)
	{
		SoundCommand(0xE2);
		if(VoiceStop) return;
		SoundCommand(0xFD);//偏低
	}
	else if(DIA>=WHODIA2)
	{
		SoundCommand(0xE2);
		if(VoiceStop) return;
		SoundCommand(0xFC);
	}
	if(VoiceStop) return;
	if(SYS>=80&&SYS<WHOSYS2&&DIA>=50&&DIA<WHODIA2)
	{
		SoundCommand(0xDF);
		if(VoiceStop) return;
		SoundCommand(0xE0);
	}
}



void Relax(unsigned char Time)
{
	uint32 SecondCount;
	SecondCount=Time*60*50;
	OUT_VALVE(0);
	PumpOff();
	DarkAllIcon();
	while(IsPowerOn&&SecondCount>0)
	{
		ADOKFlag=0;
		while(!ADOKFlag);
		calpressure();
		showpressure();
		SendPressure();
		if(!KEY_POWER) IsPowerOn=0;
		ShowRestTime(SecondCount/50);
		SecondCount--;
	}
}



void PumptoTarget(unsigned int TargetP,unsigned char Time)
{
	uint32 SecondCount;
	uint8 AddFlag,SubFlag;
	uint16 Timew;
	//uint16 express;
	uint8 PWMSet;
	PWMSet=80;
	DarkAllIcon();
	if(TargetP>300) TargetP=300;
	if(TargetP<60) TargetP=60;
	OUT_VALVE(1);
	PumpUp(250);
	calpressure();
	AutoOffTimew=0;
	do
	{
		ADOKFlag=0;
		while(!ADOKFlag);
		calpressure();
		showpressure();
		SendPressure();
		if(AutoOffTimew>1500)
		{
			ResultCode=3;
			showerror();
		}
		if(!KEY_POWER) IsPowerOn=0;
	}while(PressuremmHgw<TargetP&&IsPowerOn);
	if(!IsPowerOn) goto OVER;

	SecondCount=Time*60*50;
	//express=PressuremmHgw;
	AutoOffTimew=0;
	Timew=0;
	AddFlag=0;
	SubFlag=0;
	while(IsPowerOn&&SecondCount>0)
	{
		if(!KEY_POWER) IsPowerOn=0;
		ShowRestTime(SecondCount/50);
		ADOKFlag=0;
		while(!ADOKFlag);
		SecondCount--;
		Timew++;
		calpressure();
		showpressure();
		SendPressure();
		if(PressuremmHgw>TargetP-5)
		{
			AutoOffTimew=0;
		}
		if(AutoOffTimew>1500||PressuremmHgw>MAX_PRESSURE)
		{
			ResultCode=3;
			showerror();
		}

		if(Timew%10==0)
		{
			if(PressuremmHgw<TargetP-5)
			{
				PumpUp(240);
			}
			else if(PressuremmHgw>TargetP+5)
			{
				PumpOff();
			}
			else
			{
				if(PressuremmHgw<TargetP-3)
				{
					PWMSet++;
					AddFlag=1;
				}
				else
				{
					if(AddFlag)
					{
						PWMSet--;
						AddFlag=0;
					}
				}
				if(PressuremmHgw>TargetP+2)
				{
					PWMSet--;
					SubFlag=1;
				}
				else
				{
					if(SubFlag)
					{
						PWMSet++;
						SubFlag=0;
					}
				}
				PumpUp(PWMSet);
			}
			//if(express>PressuremmHgw) temp=express-PressuremmHgw;
			//else temp=PressuremmHgw-express;
			//express=PressuremmHgw;
		}
	}
	OVER:
	OUT_VALVE(0);
	PumpOff();
}

void showhex(uint8 x)
{
	DisplayHalfByte(x/16,DIATen);
	DisplayHalfByte(x%16,DIASingle);
}

void readfunction(void)
{
#if 1
	uint8 temp,j;
	memset(MemBack,0xFF,sizeof(MemBack));
	ReadSecData(FunctionAdd);
		for(j=0;j<8;j+=4)
		{
			inbyte[j+3] = (u8)(MemBack[j/4]>>24);
			inbyte[j+2] = (u8)(MemBack[j/4]>>16);
			inbyte[j+1] = (u8)(MemBack[j/4]>>8);
			inbyte[j] = (u8)(MemBack[j/4]);
		}
	/*
		if(!KEY_MEMORYUP)
		{
			for(j=0;j<8;j++)
			{
				showhex(inbyte[j]);
				delay(75);
			}
		}
	*/

		temp=0;
		for(j=0;j<7;j++) temp+=inbyte[j];
		if(inbyte[7]==temp)
		{
			BPLevelType		=inbyte[0]>>4;
			UartType		=inbyte[0]&0x0f;
			KeyNumber		=inbyte[1]>>4;
			VoiceType		=inbyte[1]&0x0f;
			MemorySetType	=inbyte[2]>>4;
			VoiceType2		=inbyte[2]&0x0f;
			SystemSetFlag4.b=inbyte[3];
			SystemSetFlag.b =inbyte[4];
			SystemSetFlag2.b=inbyte[5];
			SystemSetFlag3.b=inbyte[6];

			DeflateValueType=2;
			InfDefFlag	= 0;
			return;
		}
//备份无设定，用默认

	BPLevelType			=1;				//who	[BPLevelType = 8  French sys 130-139 dia 85-89 No voice WHO ]
	UartType			=2;				//1、蓝牙2、uart test 3、GPRS 4、USB唤醒和蓝牙一体机
	KeyNumber			=3;				//1、1KEY 2、2KEY 3、345KEY  4、4KEY 记忆加当作用户键 5、归CR屏蔽   6、单按键唤醒 7、银鹿	8、80V三触摸按键模式 9、记忆键反过来 10、蓝牙
	VoiceType			=1;				//0无语音 1通用中文 2英文 3土耳其语 4英文土耳其双语 5波兰语 6普通话粤语 7美语	8五国语音 9缅甸语 10 Khmer \13 中英泰
	MemorySetType		=2;				//
	DeflateValueType=2;				//漏速6.0mmhg/s
	VoiceType2			=3;				//0单泰语 1单泰语带音量调节 2中英泰 3 中英泰带音量调节  //VoiceType=1&VoiceType2=2中缅双语 \VoiceType=1&&VoiceType2=3  中英马三语带音量调节
	UnitSetEnable		=0;				//有单位设定

	AUSMode				=0;				//正常血压计模式
	PADEnable			=1;				//show hib
	TwoUser=1;								//两个用户
	RCSensorType		=1;				//1电阻式 0电容式sensor
	LinearValveFlag		=0;				//1比例阀 0快速阀
	PowerType			=0;				//1锂电   0碱性电池
	AMPMLCD				=0;				//24 hour;	1 is 12H
	NoTimer				=0;				//1无时间  0有时间
	UparmWWFlag			=1;				//臂式
	VoiceSetEnable  =0;				//有语音设定,仅限中文语音，不包括三语、双语的设定
	BTEnable			=0;				//0无蓝牙对接 1有
	InfDefFlag			=0;				//泄气量测
	AverageType			=1;				//3组平均
	BLFlag				=1;				//三色背光依WHO， =0 三色背光依脉压差
	AlarmClock			=0;				//无闹钟
	EndVoice			=0;				//关机报谢谢使用
	NoTimeSet			=0;				//上电不设置时间
	SetMode2			=0;				//短按设定改用户，长按设定单位
	LCDType				=0;				//2.0 80S的LCD
	MoveDetect			=1;				//移动侦测
	CuffDetect			=1;				//绑带松紧检测
	UnitIcon			=1;				//无KPA时 显示mmhg符号
	ThreeKeyFlag=1;
	BTIconEnable		=0;				//有蓝牙符号LCD  1

	E2Mode				=0;				//印度客户移动侦测报错

	BZEnable			=0;//侦测到心跳时蜂鸣器响
	ShowMean			=1;//显示记忆
	LCDType2			=0;//80V LCD 取消WIFI符号
	LCD80Y				=1;//80Y LCD
	StartBZ				=0;//上电时BZ声
	UserSetEnable		=1;//设定模式有没有设定用户
	DPIconDisable		=0;//银鹿LCD屏蔽动脉硬化符号
	return;

#endif
}




void spandivid(void)
{
#if 1
	uint8 temp,j;
	AA1=0;
	B1=0;
	C1=0;

	if(ReadSecData(CalAdd)==TRUE)
	{
		for(j=0;j<12;j+=4)
		{
			inbyte[j] = (u8)(MemBack[j/4]>>24);
			inbyte[j+1] = (u8)(MemBack[j/4]>>16);
			inbyte[j+2] = (u8)(MemBack[j/4]>>8);
			inbyte[j+3] = (u8)(MemBack[j/4]);
		}
		temp=0;
		for(j=0;j<7;j++) temp+=inbyte[j];
		if(inbyte[7]==temp)
		{
			C11b=inbyte[1];
			AA12b=inbyte[2];
			B11b=inbyte[3];
			AA11b=inbyte[4];
			B12b=inbyte[5];
			C12b=inbyte[6];
			//spanerrorflag=0;
			return;
		}
	}
//如果读EEPROM错误,则使用默认值.
	if(UparmWWFlag)
		{
			if(RCSensorType) 	AA1=A1UPR;
			else				AA1=AA1UP;
			B1=B1UP;
			C1=C1UP;
		}
	else
		{
			if(RCSensorType) 	AA1=A1WWR;
			else				AA1=A1WW;
			B1=B1WW;
			C1=C1WW;
		}
	//spanerrorflag=1;
#endif
 }
void showvalveoffset(void)
{
	if(valveoffset>=400)
	{
		DisplayToSYSDIA(valveoffset-400,1);
		DayIconOff;
	}
	else
	{
		DisplayToSYSDIA(400-valveoffset,1);
		DayIconOn;
	}
}
void swit_china_eng_malay(e_voice_c_e_ma_switch_t e_voice_c_e_ma_switch)//中语英语马来西亚语切换
{
    switch (e_voice_c_e_ma_switch)
    {
    case E_ENG:
        SoundAllCommand(0x11,0,0,0);//11H - - - 选择英文（上电默认英文）
        break;
    case E_MALAY:
        SoundAllCommand(0x22,0,0,0);//22H - - - 选择马来西亚
        break;
    case E_CHINA:
        SoundAllCommand(0x33,0,0,0);//33H - - - 选择中
        break;

    default:
        break;
    }
}

void swit_disp_china_eng_malay(e_voice_c_e_ma_switch_t e_voice_c_e_ma_switch)//中语英语马来西亚语切换和显示
{
	DisplayHalfByte(5,SYSTen); //SP
	DisplayHalfByte(0x1b,SYSSingle);
    swit_china_eng_malay(e_voice_c_e_ma_switch);
    switch (e_voice_c_e_ma_switch)
    {
    case E_ENG:
        DisplayHalfByte(1,DIASingle);
        break;
    case E_MALAY:
        DisplayHalfByte(2,DIASingle);
        break;
    case E_CHINA:
        DisplayHalfByte(3,DIASingle);
        break;

    default:
        break;
    }
}

void volume_adjus_china_eng_malay(uint8 volume_val_adjus)
{
    VoiceSetFlag = 1;
    swit_china_eng_malay(e_voice_c_e_ma_switch);//中语英语马来西亚语切换
    if (volume_val_adjus == 0)
    {
        SoundCommand(0xb0);//B0H 音量 0（声音最小）
        VoiceEnable = 0;
    }
    else if (volume_val_adjus == 1)
    {
        VoiceEnable = 1;
        SoundCommand(0xb2);//B2H 音量 3
        // SoundCommand(0xb3);//B3H 音量 5
    }
    else if (volume_val_adjus == 2)
    {
        VoiceEnable = 1;
        SoundCommand(0xb4);//B4H 音量 7
        // SoundCommand(0xb5);//B5H 音量 9
    }
    else if (volume_val_adjus == 3)
    {
        VoiceEnable = 1;
        SoundCommand(0xb6);//B6H 音量 11
        // SoundCommand(0xba);//BAH 音量 15（声音最大）上电默认音量
    }
    VoiceSetFlag = 0;
}
void volume_disp_china_eng_malay(uint8 volume_val)//中语英语马来西亚语音量调节和显示
{
	DisplayHalfByte(0x0d,SYSTen); //db
	DisplayHalfByte(0x19,SYSSingle);
    switch (volume_val)
    {
    case 0:
        // DisplayHalfByte(0x14,DIAHundred); //oFF
        DisplayHalfByte(0,DIATen);
        DisplayHalfByte(0x0f,DIASingle);
        break;
    case 1:
        // DisplayHalfByte(0x10,DIAHundred); //
        DisplayHalfByte(0x10,DIATen);
        DisplayHalfByte(1,DIASingle);
        break;
    case 2:
        // DisplayHalfByte(0x10,DIAHundred); //
        DisplayHalfByte(0x10,DIATen);
        DisplayHalfByte(2,DIASingle);
        break;
    case 3:
        // DisplayHalfByte(0x10,DIAHundred); //
        DisplayHalfByte(0x10,DIATen);
        DisplayHalfByte(3,DIASingle);
        break;

    default:
        break;
    }
}
